/**
 * 
 */
package com.webkaifa.tree;

import java.util.Iterator;

import java.util.Iterator;

public class TreeView {
	
	public TreeView(){}
	public TreeView(TreeNode treeNode){this.treeNode=treeNode;}
	private int checkId=-100;
	private String ckNodeName="ckNode";
	private String checkUserAttribute="";
	
	public String getCheckUserAttribute() {
		return checkUserAttribute;
	}
	public void setCheckUserAttribute(String checkUserAttribute) {
		this.checkUserAttribute = checkUserAttribute;
	}
	public String getCkNodeName() {
		return ckNodeName;
	}
	public void setCkNodeName(String ckNodeName) {
		this.ckNodeName = ckNodeName;
	}
	public int getCheckId() {
		return checkId;
	}
	public void setCheckId(int checkId) {
		this.checkId = checkId;
	}
	public String displayView(TreeNode treeNode)
	{
		String childView=getChildView(treeNode,1);
		String wholeView=this.getRootView(treeNode)+childView+this.getJsCode();
		return wholeView;
	}
	public String displayLeafCheckView(TreeNode treeNode)
	{
		String childView=getChildLeafCheckView(treeNode,1);
		String wholeView=this.getRootLeafCheckView(treeNode)+childView+this.getJsCode();
		return wholeView;
	}
	public String displayView()
	{
		return this.displayView(this.treeNode);
	}
	public String displayLeafCheckView()
	{
		return this.displayLeafCheckView(this.treeNode);
	}
	public String getExpandImg() {
		return expandImg;
	}
	public void setExpandImg(String expandImg) {
		this.expandImg = expandImg;
	}
	public String getLeafImg() {
		return leafImg;
	}
	public void setLeafImg(String leafImg) {
		this.leafImg = leafImg;
	}
	public String getLineImg() {
		return lineImg;
	}
	public void setLineImg(String lineImg) {
		this.lineImg = lineImg;
	}
	public String getShrinkImg() {
		return shrinkImg;
	}
	public void setShrinkImg(String shrinkImg) {
		 this.shrinkImg = shrinkImg;
	}
	private TreeNode treeNode=null;
	private String   imgPath="/wis/images";
	private String   CKEXP=" checked ";
	private String expandImg=imgPath+"/Darrow11.gif";
	private String shrinkImg=imgPath+"/Rarrow11.gif";
	private String leafImg=imgPath+"/darrow51.gif";
	private String lineImg=imgPath+"/darrow61.gif";
	private String branchEndImg=imgPath+"/branch_end.gif";
	private String plusEndImg=imgPath+"/plus_end.gif";
	private String lableStyle=" style=\"cursor:hand;font-family:verdana;font-size:12px\" ";
	private String onMouseOver=" onMouseOver=\"this.style.color='red'\" ";
	private String onMouseOut=" onMouseOut=\"this.style.color='black'\" ";
	private String getJsCode()
	{
	  String retView=
	   "\r\n<script>"+
	   "\r\nfunction toggleMenuDisplay(id)"+
	   "{"+
	    "var divCtl=document.getElementById('divId_'+id);" +
	    "var imgCtl=document.getElementById('imgId_'+id);"+
	    "if(divCtl.style.display == 'none'){" +
	    " imgCtl.src=\""+expandImg+"\";"+
	    " divCtl.style.display='block';" +
	    "}"+
	    "else {"+
	    " imgCtl.src=\""+shrinkImg+"\";"+
	    " divCtl.style.display='none';" +
	    "}"+
	   "}\r\n"+
	   "function onCheck(chCtl)" +
	   "{}" +
	   "</script>\r\n";
	  return retView;
	 }
	
	 private String getRootView(TreeNode treeNode)
	 {
	   String imgId=" id=\"imgId_"+treeNode.getSelfId()+"\"";
	   String checkedString="";
	   if(treeNode.getSelfId()==this.getCheckId()) checkedString=this.CKEXP;
	   String onClick=" onClick=\"toggleMenuDisplay('"+treeNode.getSelfId()+"')\"";
	   if(treeNode.isLeaf())onClick="";
	   String retView="<img src=\""+expandImg +"\" "+onClick+imgId+" width='26' height='22' align='absmiddle'>"
	    +"<input type='radio' name=\""+ckNodeName+"\" value=\""+treeNode.getSelfId()
	     +"\" "+checkedString+ checkUserAttribute+" onClick=\"onCheck(this)\">"
	     +"<label "+onClick+ onMouseOver + onMouseOut+lableStyle+">"+treeNode.getNodeName()+"</label>";
	   return retView;
	 }
	 private String getRootLeafCheckView(TreeNode treeNode)
	 {
	   String checkBox="";
	   String checkedString="";
	   if(treeNode.getSelfId()==this.getCheckId()) checkedString=this.CKEXP;
	   if(treeNode.isLeaf())checkBox="<input type='radio' name=\""+ckNodeName+"\" value=\""+treeNode.getSelfId()
	     +"\" "+checkedString+checkUserAttribute+" onClick=\"onCheck(this)\">";
	   String imgId=" id=\"imgId_"+treeNode.getSelfId()+"\"";
	   String onClick=" onClick=\"toggleMenuDisplay('"+treeNode.getSelfId()+"')\"";
	   if(treeNode.isLeaf())onClick="";
	   String retView="<img src=\""+expandImg +"\" "+onClick+imgId+" width='26' height='22' align='absmiddle'>"
	   	+checkBox
	     +"<label "+onClick+ onMouseOver + onMouseOut+lableStyle+">"+treeNode.getNodeName()+"</label>";
	   return retView;
	 }
	 private String getLeafView(TreeNode treeNode,int lastFlag)
	 {
		String checkedString="";
		if(treeNode.getSelfId()==this.getCheckId()) checkedString=this.CKEXP;
		String imgSrc=leafImg;
		if(lastFlag==1)imgSrc=this.branchEndImg;
	    String retView="<img src=\""+imgSrc+"\" width='26' height='22' align='absmiddle'>"
	    +"<input type='radio' name=\""+ckNodeName+"\" value=\""+treeNode.getSelfId()
	    +"\" "+checkedString+checkUserAttribute+" onClick=\"onCheck(this)\">"
	    +"<label"+onMouseOver + onMouseOut+lableStyle+">"+treeNode.getNodeName()+"</label><br>";
	    return retView;
	 }
	 private String getDirView(TreeNode treeNode,int lastFlag)
	 {
		 String checkedString="";
		 if(treeNode.getSelfId()==this.getCheckId()) checkedString=this.CKEXP;
		 String imgSrc=shrinkImg;
	//	 if(lastFlag==1)imgSrc=this.plusEndImg;
		 String imgId=" id=\"imgId_"+treeNode.getSelfId()+"\"";
		 String onClick=" onClick=\"toggleMenuDisplay('"+treeNode.getSelfId()+"')\"";
		 String retView="<img src=\""+imgSrc+"\" "+onClick+imgId +" width='26' height='22' align='absmiddle'>"
		 +"<input type='radio' name=\""+ckNodeName+"\" value=\""+treeNode.getSelfId()
		 +"\" "+checkedString+checkUserAttribute+" onClick=\"onCheck(this)\">"
		 +"<label "+onClick+ onMouseOver + onMouseOut+lableStyle+" >"+treeNode.getNodeName()+"</label><br>";
		 return retView;
	 }
	 private String getDirNoCheckView(TreeNode treeNode,int lastFlag)
	 {
		 String imgSrc=shrinkImg;
	//	 if(lastFlag==1)imgSrc=this.plusEndImg;
		 String imgId=" id=\"imgId_"+treeNode.getSelfId()+"\"";
		 String onClick=" onClick=\"toggleMenuDisplay('"+treeNode.getSelfId()+"')\"";
		 String retView="<img src=\""+imgSrc+"\" "+onClick+imgId +" width='26' height='22' align='absmiddle'>"
		 +"<label "+onClick+ onMouseOver + onMouseOut+lableStyle+" >"+treeNode.getNodeName()+"</label><br>";
		 return retView;
	 }
	 private String getLineView()
	 {
		 String retView="<img src=\""+lineImg+"\" width='26' height='22' align='absmiddle'>";
		 return retView;
	 }
	
	 public String getChildView(TreeNode treeNode,int depth)
	 {
		 String disPlayView="style='display:none'";
		 if(depth==1)disPlayView="style='display:block'";
		 String nodeView="<div id=\"divId_"+treeNode.getSelfId()+"\""+disPlayView+">";
		 String frontLineView="";
		 for(int i=0;i<depth;i++) frontLineView+=this.getLineView();
		 Iterator it=treeNode.getChildList().iterator();
		 while(it.hasNext())
		 {
			 TreeNode tmpNode=(TreeNode)it.next();
			 int lastFlag=0;
			 if(it.hasNext()==false)lastFlag=1;
			 if(tmpNode.isLeaf())
			 {
				 nodeView+=frontLineView+this.getLeafView(tmpNode,lastFlag);
			 }
			 else
			 {
				 nodeView+=frontLineView+this.getDirView(tmpNode,lastFlag)+getChildView(tmpNode,depth+1); 
			 }
		 }
		 return nodeView+="</div>"; 
	 }
	 public String getChildLeafCheckView(TreeNode treeNode,int depth)
	 {
		String disPlayView="style='display:none'";
	  	if(depth==1)disPlayView="style='display:block'";
	  	String nodeView="<div id=\"divId_"+treeNode.getSelfId()+"\""+disPlayView+">";
	  	String frontLineView="";
	  	for(int i=0;i<depth;i++) frontLineView+=this.getLineView();
	  	Iterator it=treeNode.getChildList().iterator();
	  	while(it.hasNext())
	  	{
		  TreeNode tmpNode=(TreeNode)it.next();
		  int lastFlag=0;
		  if(it.hasNext()==false)lastFlag=1;
		  if(tmpNode.isLeaf())
		  {
			  nodeView+=frontLineView+this.getLeafView(tmpNode,0);
		  }
		  else
		  {
			  nodeView+=frontLineView+this.getDirNoCheckView(tmpNode,0)+getChildLeafCheckView(tmpNode,depth+1); 
		  }
	  	}
	  	return nodeView+="</div>"; 
	 }
	public TreeNode getTreeNode() {
		return treeNode;
	}
	public void setTreeNode(TreeNode treeNode) {
		this.treeNode = treeNode;
	}
	public String getBranchEndImg() {
		return branchEndImg;
	}
	public void setBranchEndImg(String branchEndImg) {
		this.branchEndImg = branchEndImg;
	}
	public String getPlusEndImg() {
		return plusEndImg;
	}
	public void setPlusEndImg(String plusEndImg) {
		this.plusEndImg = plusEndImg;
	}
	public String getImgPath() {
		return imgPath;
	}
	public void setImgPath(String imgPath) {
		this.imgPath = imgPath;
	}	
}